package com.yt.supplier.api.client;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import com.yt.supplier.models.bo.ProductInfoSaveBO;
import com.yt.supplier.models.dto.SupplierInfoDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import com.yt.supplier.models.service.IProductInfoService;
import com.yt.supplier.models.bo.ProductInfoBO;
import com.yt.supplier.models.dto.ProductInfoDTO;

import java.util.List;

/**
 * 产品信息控制器
 *
 * @author lizicong
 * @Date 2025-09-10 10:05:47
 */

@RestController
@RequestMapping("/api/productInfo")
@Slf4j
@Api(value = "ProductInfoApiController",tags = "产品信息Api接口")
public class ProductInfoApiController {

    @DubboReference
    private IProductInfoService productInfoService;

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<ProductInfoDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        ProductInfoDTO dto = productInfoService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "产品信息列表",notes = "产品信息列表")
    @PostMapping("/queryPageList")
    public Result<Page<ProductInfoDTO>> queryPageList(ProductInfoBO bo){
        Wrapper<ProductInfoDTO> wrapper = new EntityWrapper<>();
                if (bo.getStatus() != null){
                    wrapper.eq("status", bo.getStatus());
                }
        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<ProductInfoDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = productInfoService.selectPage(page, wrapper);

        return new Result<>(page);
    }
    @ApiOperation(value = "添加产品信息",notes = "添加产品信息")
    @PostMapping(value = "/add")
    public Result insert(@RequestBody ProductInfoSaveBO saveBO) {
        boolean flag = productInfoService.add(saveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }
    @ApiOperation(value = "修改产品信息",notes = "修改产品信息")
    @PostMapping("/update")
    public Result update(@RequestBody ProductInfoSaveBO saveBO){
        ProductInfoDTO dto = new ProductInfoDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setId(saveBO.getId());
        boolean flag = productInfoService.updateById(dto);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }
    @ApiOperation(value = "删除产品信息",notes = "删除产品信息")
    @ApiImplicitParam(name = "id",value = "产品信息id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id){
        boolean flag = productInfoService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }
    @ApiOperation(value = "产品信息列表",notes = "产品信息列表")
    @PostMapping("/productInfolist")
    public Result<List<ProductInfoDTO>> supplierlist(){

        List<ProductInfoDTO> productInfoDTOList = productInfoService.selectList(null);
        if (productInfoDTOList!= null){
            System.out.println(" supplierInfoDTOList "+productInfoDTOList);
            return new Result<>(productInfoDTOList);
        } else {
            return new Result<>(CodeEnum.FAIL_100005);
        }
    }
    @ApiOperation(value = "搜索产品信息", notes = "根据关键词搜索 产品信息")
    @ApiImplicitParam(name = "keyword", value = "搜索关键词（产品名称）",
            required = true, paramType = "query", dataType = "String")
    @PostMapping("/searchproductinfo") // 路径无参数，固定为 /searchsupplier
    public Result<List<ProductInfoDTO>> searchproductinfo(@RequestParam("keyword") String keyword) { // 接收查询参数
        System.out.println("keyword: " + keyword);
        // 注意：这里需要手动拼接 % 实现模糊查询（之前的问题）
        List<ProductInfoDTO> productInfoDTOList = productInfoService.SearchProductInfolist(keyword );
        if (productInfoDTOList == null) {
            return new Result<>(CodeEnum.FAIL_100005);
        }
        System.out.println("productInfoDTOList: " + productInfoDTOList);
        return new Result<>(productInfoDTOList);
    }
}
